package com.kh.statistics.video.helper;

import com.kh.statistics.video.core.ExcelExport;
import com.kh.statistics.video.exception.BizException;
import com.kh.statistics.video.exception.ResponseStatusEnum;
import org.apache.commons.lang.StringUtils;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @BelongsProject: statistics
 * @BelongsPackage: com.kh.statistics.video.helper
 * @Author: Hong.Kong
 * @CreateTime: 2024-05-10  10:38
 * @Description: TODO
 * @Version: 1.0
 */
public class ExcelHelper {

    public static String getExcelName(String accountName, String defaultName) {
        if (StringUtils.isBlank(defaultName)) {
            throw new BizException(ResponseStatusEnum.BAD_REQUEST.getCode(), "未设置默认名称");
        }
        if (StringUtils.isNotBlank(accountName)) {
            return accountName + defaultName;
        }
        return defaultName;
    }

    public static <T> void export(String sheetName, List<T> list, Class<T> clazz) {
        HttpServletResponse response = HttpServletHelper.getResponse();
        try {
            HttpServletHelper.setExcelResponseProp(response, sheetName);
            ServletOutputStream outputStream = response.getOutputStream();
            ExcelExport.export(outputStream, sheetName, list, clazz);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
